package weixin_new.bean;  

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.MessageDigest;  
import java.security.NoSuchAlgorithmException;  
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Arrays;  
import java.util.Date;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class Weixin_base {  

	/**
	 * 促销活动打卡
	 * @param PicUrl //照片地址
	 * @param fromUserName //发送照片的微信id
	 * @return
	 * @throws IOException 
	 * @throws JSONException 
	 */
	public static String add_Cxhd_dk(String PicUrl, String fromUserName) throws IOException, JSONException {  

		String inStr ="code=200035&inStr="+PicUrl+"</C>"+fromUserName+"&user_code=adrd&user_passwd=adrd";
		URL url = new URL("http://mp.antradars.com/myapp/WeixinServlet");  
		//根据不同的网关，调用不同的方法
		HttpURLConnection connection =(HttpURLConnection) url.openConnection();
		connection.setDoOutput(true);   //一定要设为true,因为要发送数据

		//下面开始设定Http头
		OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");

		out.write(inStr);
		out.flush();
		out.close();

		InputStream in = connection.getInputStream();
		BufferedReader reader = new BufferedReader(new InputStreamReader(in,"utf-8") );
		String inline = "";

		String fanhui = "";


		while((inline=reader.readLine())!=null){

			JsonParser parser=new JsonParser();  //创建JSON解析器
			JsonObject object=(JsonObject) parser.parse(inline);  //创建JsonObject对象
			String bz = object.get("bz").getAsString();

			if(bz.equals("-1")){
				fanhui+=bz+"</C>-1</C>";
			}else if(bz.equals("1")){
				String hdxx = object.get("hdxx").getAsString();
				fanhui+=bz+"</C>"+hdxx+"</C>";
			}

		}

		in.close();
		connection.disconnect();

		System.out.println("fanhui====="+fanhui);

		return fanhui;
	}  

	/**
	 * 查询该用户是否参加了促销活动
	 * @param fromUserName
	 * @return
	 * @throws IOException
	 * @throws JSONException
	 */

	public static String Query_Cxhd(String fromUserName) throws IOException, JSONException {  

		String inStr ="code=200037&inStr="+fromUserName+"&user_code=adrd&user_passwd=adrd";
		URL url = new URL("http://mp.antradars.com/myapp/WeixinServlet");  
		//根据不同的网关，调用不同的方法
		HttpURLConnection connection =(HttpURLConnection) url.openConnection();
		connection.setDoOutput(true);   //一定要设为true,因为要发送数据

		//下面开始设定Http头
		OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");

		out.write(inStr);
		out.flush();
		out.close();

		InputStream in = connection.getInputStream();
		BufferedReader reader = new BufferedReader(new InputStreamReader(in,"utf-8") );
		String inline = "";

		String fanhui = "";


		while((inline=reader.readLine())!=null){

			JsonParser parser=new JsonParser();  //创建JSON解析器
			JsonObject object=(JsonObject) parser.parse(inline);  //创建JsonObject对象
			String bz = object.get("bz").getAsString();

			if(bz.equals("-1")){
				fanhui+=bz+"</C>-1</C>";
			}else if(bz.equals("1")){
				String hdxx = object.get("hdxx").getAsString();
				fanhui+=bz+"</C>"+hdxx+"</C>";
			}

		}

		in.close();
		connection.disconnect();

		return fanhui;
	}  

	/**
	 * 增加微信消息
	 * @param PicUrl
	 * @param fromUserName
	 * @return 
	 * @throws IOException
	 * @throws JSONException
	 */
	public static String add_weixin_xinxi(String neirong, String fromUserName,String MsgType) throws IOException, JSONException {  

		Connection con = null;
		PreparedStatement pstm=null;//保存预处理语句
		ResultSet rs=null;//保存查询的结果集
		String transcode = "";
		DataSource ds=null ;
		Context ctx;
		try {
			ctx = new InitialContext();

			ds =(DataSource) ctx.lookup("java:comp/env/jdbc/myapp");
			con = ds.getConnection();
			
			//获得今天的日期
			SimpleDateFormat sdf2 = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String dqsj = sdf2.format(new Date()); 

			
			StringBuffer sqlSB =new StringBuffer();
			sqlSB.append("insert into t_weixin_message (OpenID,neirong,MsgType,lrsj)values(?,?,?,?)");
			pstm = con.prepareStatement(sqlSB.toString());
			pstm.setString(1,fromUserName);
			pstm.setString(2,neirong);
			pstm.setString(3,MsgType);
			pstm.setString(4,dqsj);
			pstm.execute();
			base.ResourceClose.close(rs, pstm, null, con);
			System.out.println("insert into t_weixin_message (OpenID,neirong,MsgType,lrsj)values("+fromUserName+","+neirong+","+MsgType+",?)");
			//
			//		URL url = new URL("http://mp.antradars.com/myapp/WeixinServlet");  
			//		//根据不同的网关，调用不同的方法
			//		HttpURLConnection connection =(HttpURLConnection) url.openConnection();
			//		connection.setConnectTimeout(2000);// 设置连接超时时间，单位毫秒  
			//		connection.setReadTimeout(2000);// 设置读取数据超时时间，单位毫秒  
			//		connection.setDoOutput(true);   //一定要设为true,因为要发送数据
			//
			//		//下面开始设定Http头
			//		OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
			//		out.write(inStr);
			//		out.flush();
			//		out.close();

//			URL url = new URL("http://mp.antradars.com/myapp/WeixinServlet");  
//			//		//根据不同的网关，调用不同的方法
//			HttpURLConnection connection =(HttpURLConnection) url.openConnection();
//
//			connection = (HttpURLConnection) url.openConnection();// 新建连接实例  
//			connection.setConnectTimeout(2000);// 设置连接超时时间，单位毫秒  
//			connection.setReadTimeout(2000);// 设置读取数据超时时间，单位毫秒  
//			connection.setDoOutput(true);// 是否打开输出流 true|false  
//			connection.setDoInput(true);// 是否打开输入流true|false  
//			connection.setRequestMethod("POST");// 提交方法POST|GET  
//			connection.setUseCaches(false);// 是否缓存true|false
//			connection.setRequestProperty("Accept-Charset", "UTF-8");
//			connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
//			connection.connect();// 打开连接端口  
//			DataOutputStream out = new DataOutputStream(connection.getOutputStream());// 打开输出流往对端服务器写数据  
//			out.writeBytes(inStr);// 写数据,也就是提交你的表单 name=xxx&pwd=xxx  
//			out.flush();// 刷新  
//			out.close();// 关闭输出流  
//
//			String inline="";
//
//			InputStream in = connection.getInputStream();
//			BufferedReader reader = new BufferedReader(new InputStreamReader(in,"utf-8") );
//			while((inline=reader.readLine())!=null){
//				System.out.println("inline====="+inline);
//			}
//
//			in.close();
//			connection.disconnect();

		} catch (NamingException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return "1";
	}  



}  